
//
//  BBImageBrowser.m
//  ZTBangBao
//
//  Created by ZYJ on 15/8/10.
//  Copyright (c) 2015年 BangBao. All rights reserved.
//

#import "BBImageBrowser.h"
#define SC_DEVICE_BOUNDS    [[UIScreen mainScreen] bounds]
#define RGBCOLOR(r,g,b,a)          [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:a]
//#import "UIImageView+NetImage.h"
@implementation BBImageBrowser
{
    UIImageView *_imageView;
}
- (void)showWithImage:(UIImage *)avatarImage {
    [self showImageWithImage:avatarImage orImageUrlStr:nil];
}
- (void)showImage:(UIImageView *)avatarImageView
{
    [self showImageWithImage:avatarImageView.image orImageUrlStr:nil];
}
- (void)deleteImageBtnClick {
    _deleteBtnClickblock();
    [self hideSelfView];
}
- (void)hideSelfView {
    self.hidden = YES;
}
- (void)hideImage:(UITapGestureRecognizer*)tap{
    UIView *backgroundView=tap.view;
    UIImageView *imageView=(UIImageView*)[tap.view viewWithTag:1];
    [UIView animateWithDuration:0.3 animations:^{
        imageView.frame=CGRectMake(0, 0, screenW, screenW);
        backgroundView.alpha=0;
    } completion:^(BOOL finished) {
        [backgroundView removeFromSuperview];
        self.hidden = YES;
    }];
}
- (void)showImageWithImage:(UIImage *)image orImageUrlStr:(NSString *)imageUrlStr
{
    if (self.superview == nil) {
        self.frame = SC_DEVICE_BOUNDS;
        self.backgroundColor = RGBCOLOR(1, 1, 1, 1.0);
        [[Utility mainWindow] addSubview:self];
    } else self.hidden = NO;
    
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = self.bounds;
    scrollView.delegate = self;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.decelerationRate = UIScrollViewDecelerationRateFast;
    scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self addSubview:scrollView];
    
    _imageView = [[UIImageView alloc] init];
    _imageView.frame = CGRectMake(0, 0, screenW, screenW);
    _imageView.center = self.center;
    if (image) {
        _imageView.image = image;
    }
    if (imageUrlStr) {
//        [_imageView setImageWithStr:imageUrlStr placeholderImage:nil];
    }
    _imageView.contentMode = UIViewContentModeScaleAspectFit;
    [scrollView addSubview:_imageView];
    
    scrollView.maximumZoomScale = 2.0;
    scrollView.minimumZoomScale = 1.0;
    scrollView.zoomScale = 1.0;
    
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideImage:)];
    [scrollView addGestureRecognizer: tap];
    
    [UIView animateWithDuration:0.3 animations:^{
        _imageView.frame = CGRectMake(0, 0, screenW, screenW);
        _imageView.center = self.center;
        scrollView.alpha=1;
    } completion:^(BOOL finished) {
        
    }];
    
    
    UIView *navView = [[UIView alloc] init];
    navView.backgroundColor = [UIColor colorWithHexString:@"#fead00"];
    navView.frame = CGRectMake(0, 0, screenW, navHeight);
    if (_isShowNavBar) {
        [self addSubview:navView];
    }
    
    UIButton *leftItemBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 20, navHeight, navHeight - 20)];
    [leftItemBtn setImage:[UIImage imageNamed:@"navagation_back"] forState:UIControlStateNormal];
    [leftItemBtn addTarget:self action:@selector(hideSelfView) forControlEvents:UIControlEventTouchUpInside];
    [navView addSubview:leftItemBtn];
    UIButton *delBtn = [[UIButton alloc] initWithFrame:CGRectMake(screenW - navHeight, 20, navHeight, navHeight - 20)];
    [delBtn setImage:[UIImage imageNamed:@"ic_delete_healthyPoint_4"] forState:UIControlStateNormal];
    [delBtn addTarget:self action:@selector(deleteImageBtnClick) forControlEvents:UIControlEventTouchUpInside];
    if (!_isHideDeleteBtn) {
        [navView addSubview:delBtn];
    }
}
- (void)showImageWithImageUtlStr:(NSString *)imageUrlStr
{
    [self showImageWithImage:nil orImageUrlStr:imageUrlStr];
}
#pragma mark - UIScrollViewDelegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return _imageView;
}
@end
